package com.roncoo.education.course.service.admin.biz;

import java.util.List;

import javax.validation.constraints.NotNull;

import org.springframework.stereotype.Component;

import com.roncoo.education.common.core.base.Page;
import com.roncoo.education.common.core.base.PageUtil;
import com.roncoo.education.common.core.base.Result;
import com.roncoo.education.common.core.enums.SmsPlatformEnum;
import com.roncoo.education.common.core.enums.StoragePlatformEnum;
import com.roncoo.education.common.core.enums.VodPlatformEnum;
import com.roncoo.education.common.core.tools.BeanUtil;
import com.roncoo.education.common.service.BaseBiz;
import com.roncoo.education.course.dao.OptionsDao;
import com.roncoo.education.course.dao.impl.mapper.entity.Options;
import com.roncoo.education.course.dao.impl.mapper.entity.OptionsExample;
import com.roncoo.education.course.dao.impl.mapper.entity.OptionsExample.Criteria;
import com.roncoo.education.course.service.admin.req.AdminOptionsEditReq;
import com.roncoo.education.course.service.admin.req.AdminOptionsListReq;
import com.roncoo.education.course.service.admin.req.AdminOptionsPageReq;
import com.roncoo.education.course.service.admin.req.AdminOptionsSaveReq;
import com.roncoo.education.course.service.admin.resp.AdminOptionsListResp;
import com.roncoo.education.course.service.admin.resp.AdminOptionsPageResp;
import com.roncoo.education.course.service.admin.resp.AdminOptionsViewResp;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import lombok.RequiredArgsConstructor;

/**
 * ADMIN-资源（实验、课程、全景...）分类配置
 *
 * @author wujing
 */
@Component
@RequiredArgsConstructor
public class AdminOptionsBiz extends BaseBiz {

    @NotNull
    private final OptionsDao dao;

    /**
     * 资源（实验、课程、全景...）分类配置分页
     *
     * @param req 资源（实验、课程、全景...）分类配置分页查询参数
     * @return 资源（实验、课程、全景...）分类配置分页查询结果
     */
    public Result<Page<AdminOptionsPageResp>> page(AdminOptionsPageReq req) {
        OptionsExample example = new OptionsExample();
        Criteria c = example.createCriteria();
        Page<Options> page = dao.page(req.getPageCurrent(), req.getPageSize(), example);
        Page<AdminOptionsPageResp> respPage = PageUtil.transform(page, AdminOptionsPageResp.class);
        return Result.success(respPage);
    }

    /**
     * 资源（实验、课程、全景...）分类配置添加
     *
     * @param req 资源（实验、课程、全景...）分类配置
     * @return 添加结果
     */
    public Result<String> save(AdminOptionsSaveReq req) {
        Options record = BeanUtil.copyProperties(req, Options.class);
        if (dao.save(record) > 0) {
            return Result.success("操作成功");
        }
        return Result.error("操作失败");
    }

    /**
     * 资源（实验、课程、全景...）分类配置查看
     *
     * @param id 主键ID
     * @return 资源（实验、课程、全景...）分类配置
     */
    public Result<AdminOptionsViewResp> view(Long id) {
        return Result.success(BeanUtil.copyProperties(dao.getById(id), AdminOptionsViewResp.class));
    }

    /**
     * 资源（实验、课程、全景...）分类配置修改
     *
     * @param req 资源（实验、课程、全景...）分类配置修改对象
     * @return 修改结果
     */
    public Result<String> edit(AdminOptionsEditReq req) {
        Options record = BeanUtil.copyProperties(req, Options.class);
        if (dao.updateById(record) > 0) {
            return Result.success("操作成功");
        }
        return Result.error("操作失败");
    }

    /**
     * 资源（实验、课程、全景...）分类配置删除
     *
     * @param id ID主键
     * @return 删除结果
     */
    public Result<String> delete(Long id) {
        if (dao.deleteById(id) > 0) {
            return Result.success("操作成功");
        }
        return Result.error("操作失败");
    }
    
    public Result<List<AdminOptionsListResp>> list(AdminOptionsListReq req) {
        OptionsExample example = new OptionsExample();
        example.setOrderByClause("sort asc, id desc");

        Criteria c = example.createCriteria();
        if (ObjectUtil.isNotEmpty(req.getCategory())) {
            c.andCategoryEqualTo(req.getCategory());
        }

        List<Options> configList = dao.listByExample(example);
        return Result.success(BeanUtil.copyProperties(configList, AdminOptionsListResp.class));
    }
}
